package com.ssy.lingxi.product.controller.pc;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.common.constant.CommonConstant;
import com.ssy.lingxi.common.constant.Constants;
import com.ssy.lingxi.common.constant.manage.ShopTypeEnum;
import com.ssy.lingxi.common.constant.member.MemberTypeEnum;
import com.ssy.lingxi.common.constant.product.CheckTypeEnum;
import com.ssy.lingxi.common.constant.product.CommodityStatusEnum;
import com.ssy.lingxi.common.constant.product.PriceTypeEnum;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.MemberAndRoleIdDTO;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.component.redis.service.IRedisStringUtils;
import com.ssy.lingxi.platform.template.api.model.dto.inner.OpenMroInnerDTO;
import com.ssy.lingxi.product.api.model.request.*;
import com.ssy.lingxi.product.api.model.request.commodity.*;
import com.ssy.lingxi.product.api.model.response.CommodityDraftResponse;
import com.ssy.lingxi.product.api.model.response.CommodityPriceRecordResponse;
import com.ssy.lingxi.product.api.model.response.OftenBuyCommodityResponse;
import com.ssy.lingxi.product.api.model.response.commodity.*;
import com.ssy.lingxi.product.common.enums.CommodityTypeEnum;
import com.ssy.lingxi.product.entity.commodity.*;
import com.ssy.lingxi.product.entity.commodity.member.CommodityMember;
import com.ssy.lingxi.product.entity.commodity.member.CommodityUnitPriceStrategy;
import com.ssy.lingxi.product.service.IChannelCommodityService;
import com.ssy.lingxi.product.service.ICommodityService;
import com.ssy.lingxi.product.service.customer.ICustomerAttributeValueService;
import com.ssy.lingxi.product.service.customer.ICustomerCategoryService;
import com.ssy.lingxi.product.service.platform.ICategoryService;
import com.ssy.lingxi.product.utils.FileUtil;
import com.ssy.lingxi.product.utils.QRCodeUtil;
import com.ssy.lingxi.product.utils.ZipUtil;
import org.apache.commons.lang.StringUtils;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @menu: 商品管理
 * 商品管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/28
 */
@RestController
@RequestMapping("/product/commodity")
public class CommodityController extends BaseController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private IChannelCommodityService channelCommodityService;

    @Resource
    private ICustomerCategoryService customerCategoryService;

    @Resource
    private ICategoryService categoryService;

    @Resource
    private ICustomerAttributeValueService customerAttributeValueService;

    @Resource
    private IRedisStringUtils redisStringUtils;

    /**
     * 查询商品信息--商品能力
     * @param id 商品id
     * @return
     */
    @RequestMapping(value = "/getCommodity", method = RequestMethod.GET)
    public Wrapper<CommodityDetailResponse> getCommodity(@RequestParam Long id) {
        Commodity commodity = commodityService.getCommodity(id);
        if(commodity != null){
            CommodityDetailResponse commodityDetailResponse = this.modelMapper.map(commodity, CommodityDetailResponse.class);
            //按属性id排序
            commodityDetailResponse.getUnitPriceAndPicList().forEach(unitPriceAndPicResponse -> {
                List<GoodsAttributeResponse> collect = unitPriceAndPicResponse.getAttributeAndValueList().stream().sorted(Comparator.comparing(GoodsAttributeResponse::getId).reversed()).collect(Collectors.toList());
                unitPriceAndPicResponse.setAttributeAndValueList(collect);
            });

            CustomerCategoryResponse customerCategoryResponse = commodityDetailResponse.getCustomerCategory();
            if(customerCategoryResponse != null){
                customerCategoryResponse.setFullName(customerCategoryService.getFullNameByFullId(customerCategoryResponse.getFullId()));

                CategoryResponse categoryResponse = customerCategoryResponse.getCategory();
                if(categoryResponse != null){
                    categoryResponse.setFullName(categoryService.getFullNameByFullId(categoryResponse.getFullId()));
                }
            }
            //查询该商品上架的商城
            List<CommodityShop> shopList = commodity.getCommodityShopList();
            if(shopList != null && shopList.size() > 0){
                String[] fields = shopList.stream().map(shop -> String.valueOf(shop.getShopId())).toArray(String[]::new);
                List<String> list = redisStringUtils.hMGet(Constants.REDIS_KEY_SHOP, fields, Constants.REDIS_PLATFORM_MANAGE_INDEX);
                List<CommodityShopResponse> resultList = list.stream().map(value -> {
                    if (StringUtils.isNotEmpty(value)) {
                        JSONObject jsonObject = JSONUtil.parseObj(value);
                        Long shopId = jsonObject.getLong("id");
                        Integer type = jsonObject.getInt("type");
                        Integer environment = jsonObject.getInt("environment");
                        String name = jsonObject.getStr("name");
                        String logoUrl = jsonObject.getStr("logoUrl");
                        CommodityShopResponse shopResponse = new CommodityShopResponse();
                        shopResponse.setShopId(shopId);
                        shopResponse.setType(type);
                        shopResponse.setEnvironment(environment);
                        shopResponse.setIsCheck(true);
                        shopResponse.setName(name);
                        shopResponse.setLogoUrl(logoUrl);
                        return shopResponse;
                    } else {
                        return null;
                    }
                }).filter(Objects::nonNull).collect(Collectors.toList());
                commodityDetailResponse.setCommodityShopList(resultList);
            }
            return Wrapper.success(commodityDetailResponse);
        }else{
            throw new BusinessException(ResponseCode.PRODUCT_COMMODITY_NOT_EXIST);
        }
    }

    /**
     * 查询商品列表--商品能力
     * @param pageVO 分页实体
     * @param commodityQueryRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityListResponse>> getCommodityList(PageVO pageVO, CommodityQueryRequest commodityQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        commodityQueryRequest.setMemberId(sysUser.getMemberId());
        commodityQueryRequest.setMemberRoleId(sysUser.getMemberRoleId());
        Page<Commodity> result = commodityService.getCommodityList(pageVO, commodityQueryRequest);
        List<CommodityListResponse> resultList = result.getContent().stream().map(commodity -> {
            CommodityListResponse commodityListResponse = BeanUtil.copyProperties(commodity, CommodityListResponse.class);
            Integer status = commodityListResponse.getStatus();
            Long upperCommodityId = commodityListResponse.getUpperCommodityId();
            switch (status) {
                case 1:
                    commodityListResponse.setIsSubmit(true);
                    commodityListResponse.setIsUpdate(true);
                    commodityListResponse.setIsDelete(true);
                    break;
                case 2:
                    break;
                case 3:
                    commodityListResponse.setIsUpdate(true);
                    break;
                //状态4和6是一样的
                case 4:
                case 6:
                    commodityListResponse.setIsOnPublish(true);
                    break;
                case 5:
                    commodityListResponse.setIsOffPublish(true);
                    break;
            }
            if(upperCommodityId != null && upperCommodityId > 0){
                commodityListResponse.setIsUpperCommodity(true);
            }
            return commodityListResponse;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }


    /**
     * 校验是否开启MRO模式
     *
     * @param openMroInnerDTO
     * @return
     */
    @PostMapping("/checkIsOpenMro")
    public Wrapper<Boolean> checkIsOpenMro(@RequestBody OpenMroInnerDTO openMroInnerDTO) {
        return Wrapper.success(commodityService.checkIsOpenMro(openMroInnerDTO));
    }

    /**
     * 查询待审核商品列表--商品能力
     * @param pageVO 分页实体
     * @param commodityQueryRequest 查询条件实体
     * @return 操作结果
     */
    @RequestMapping(value = "/getUnCheckCommodityList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityListResponse>> getUnCheckCommodityList(PageVO pageVO, CommodityQueryRequest commodityQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        commodityQueryRequest.setMemberId(sysUser.getMemberId());
        commodityQueryRequest.setMemberRoleId(sysUser.getMemberRoleId());
        commodityQueryRequest.setStatusList(Arrays.asList(2));
        commodityQueryRequest.setCheckType(CheckTypeEnum.MEMBER_CHECK.getCode());
        Page<Commodity> result = commodityService.getCommodityList(pageVO, commodityQueryRequest);
        List<CommodityListResponse> resultList = result.getContent().stream().map(commodity -> {
            CommodityListResponse commodityListResponse = BeanUtil.copyProperties(commodity, CommodityListResponse.class);
            Integer status = commodityListResponse.getStatus();
            Long upperCommodityId = commodityListResponse.getUpperCommodityId();
            switch (status) {
                case 1:
                    commodityListResponse.setIsSubmit(true);
                    commodityListResponse.setIsUpdate(true);
                    commodityListResponse.setIsDelete(true);
                    break;
                case 2:
                    break;
                case 3:
                    commodityListResponse.setIsUpdate(true);
                    break;
                //状态4和6是一样的
                case 4:
                case 6:
                    commodityListResponse.setIsOnPublish(true);
                    commodityListResponse.setIsUpdate(true);
                    break;
                case 5:
                    commodityListResponse.setIsOffPublish(true);
                    break;
            }
            if(upperCommodityId != null && upperCommodityId > 0){
                commodityListResponse.setIsUpperCommodity(true);
            }
            return commodityListResponse;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }
    /**
     * 查询上游供应商品列表--商品能力
     * @param pageVO 分页实体
     * @param commodityUpperQueryRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getUpperCommodityList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityResponse>> getUpperCommodityList(PageVO pageVO, CommodityUpperQueryRequest commodityUpperQueryRequest) {
        CommodityQueryRequest commodityQueryRequest = new CommodityQueryRequest();
        //登录用户
        UserLoginCacheDTO sysUser = this.getSysUser();
        //上游供应商
        List<MemberAndRoleIdDTO> upperVendorMembers = sysUser.getSubVendorMembers();
        if(upperVendorMembers != null && upperVendorMembers.size() > 0){
            List<Long> parentMemberIdList = upperVendorMembers.stream().map(MemberAndRoleIdDTO::getMemberId).collect(Collectors.toList());
            commodityQueryRequest.setParentMemberIdList(parentMemberIdList);
            //页面参数
            Integer priceType = commodityUpperQueryRequest.getPriceType();
            String brandName = commodityUpperQueryRequest.getBrandName();
            String customerCategoryName = commodityUpperQueryRequest.getCustomerCategoryName();
            String memberName = commodityUpperQueryRequest.getMemberName();
            String name = commodityUpperQueryRequest.getName();
            commodityQueryRequest.setName(name);
            commodityQueryRequest.setBrandName(brandName);
            commodityQueryRequest.setMemberName(memberName);
            commodityQueryRequest.setCustomerCategoryName(customerCategoryName);
            //价格类型
            List<Integer> priceTypeList = new ArrayList<>();
            if(priceType != null && priceType > 0){
                priceTypeList.add(priceType);
            }else{
                priceTypeList.add(PriceTypeEnum.Cash.getCode());
                priceTypeList.add(PriceTypeEnum.Consult.getCode());
            }
            commodityQueryRequest.setPriceTypeList(priceTypeList);
            //状态
            List<Integer> statusList = new ArrayList<>();
            statusList.add(CommodityStatusEnum.Pass.getCode());
            statusList.add(CommodityStatusEnum.On_Shelf.getCode());
            statusList.add(CommodityStatusEnum.Off_Shelf.getCode());
            commodityQueryRequest.setStatusList(statusList);

            Page<Commodity> result = commodityService.getCommodityList(pageVO, commodityQueryRequest);
            List<CommodityResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<CommodityResponse>>(){}.getType());
            return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
        }else{
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }
    }

    /**
     * 新增/修改商品--商品能力
     * @param commodityRequest 商品实体
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateCommodity", method = RequestMethod.POST)
    public Wrapper<Long> saveOrUpdateCommodity(@RequestBody CommodityAddRequest commodityRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        customerAttributeValueService.saveCustomerAttributeValue(sysUser, commodityRequest);
        return Wrapper.success(commodityService.saveOrUpdateCommodity(sysUser, commodityRequest));
    }

    /**
     * 复制商品--商品能力
     * @param simpleIdRequest
     * @return
     */
    @RequestMapping(value = "/copyCommodity", method = RequestMethod.POST)
    public Wrapper<String> copyCommodity(@RequestBody SimpleIdRequest simpleIdRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();

        SimpleIdListRequest simpleIdListRequest = new SimpleIdListRequest();
        List<Long> idList = new ArrayList<>();
        idList.add(simpleIdRequest.getId());
        simpleIdListRequest.setIdList(idList);
        return Wrapper.success(commodityService.copyCommodity(sysUser, simpleIdListRequest, null));
    }

    /**
     * 批量新增上游会员商品--商品能力
     * @param simpleIdListRequest
     * @return
     */
    @RequestMapping(value = "/saveUpperCommodity", method = RequestMethod.POST)
    public Wrapper<String> saveUpperCommodity(@RequestBody SimpleIdListRequest simpleIdListRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.copyCommodity(sysUser, simpleIdListRequest, CommodityTypeEnum.Upper.getCode()));
    }

    /**
     * 批量删除商品--商品能力
     * @param simpleIdListRequest
     * @return
     */
    @RequestMapping(value = "deleteBatchCommodity", method = RequestMethod.POST)
    public Wrapper<String> deleteBatchCommodity(@RequestBody SimpleIdListRequest simpleIdListRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.deleteBatchCommodity(sysUser, simpleIdListRequest.getIdList()));
    }

    /**
     * 提交审核商品--商品能力
     * @param simpleIdRequest
     * @return
     */
    @RequestMapping(value = "/applyCheckCommodity", method = RequestMethod.POST)
    public Wrapper<String> applyCheckCommodity(@RequestBody SimpleIdRequest simpleIdRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.applyCheckCommodity(sysUser, simpleIdRequest.getId()));
    }

    /**
     * 获取上架商城(单个商品上架)--商品能力
     * @param id 商品id
     * @return
     */
    @RequestMapping(value = "/getShop", method = RequestMethod.GET)
    public Wrapper<List<CommodityShopResponse>> getShop(@RequestParam("id") Long id) {
        //查询该商品上架的商城
        Commodity commodity = commodityService.getCommodity(id);
        if(commodity != null){
            //获取所有非自营商城
            List<CommodityShopResponse> commodityShopList = this.getCommodityShopList(commodity, Constants.REDIS_KEY_SHOP, false);
            //获取所有自营商城
            List<CommodityShopResponse> commoditySelfShopList = this.getCommodityShopList(commodity, Constants.REDIS_KEY_SELF_SHOP + "_" + commodity.getMemberId() + "_" + commodity.getMemberRoleId(), true);
            commodityShopList.addAll(commoditySelfShopList);
            return Wrapper.success(commodityShopList);
        }else{
            throw new BusinessException(ResponseCode.PRODUCT_COMMODITY_NOT_EXIST);
        }
    }

    /**
     * 获取商城(单个商品上架需要勾中已上架的商城)
     */
    private List<CommodityShopResponse> getCommodityShopList(Commodity commodity, String redisKey, Boolean isSelfShop){
        List<CommodityShopResponse> resultList = new ArrayList<>();
        Integer priceType = commodity.getPriceType();
        List<CommodityShop> shopList = commodity.getCommodityShopList();
        //获取商品对应的上架商城
        Map<String, String> shopMap = redisStringUtils.hGetAll(redisKey, Constants.REDIS_PLATFORM_MANAGE_INDEX);
        //过滤企业商城和渠道商城，并且为启用的状态
        for (Map.Entry<String, String> entry : shopMap.entrySet()) {
            String value = entry.getValue();
            JSONObject jsonObject = JSONUtil.parseObj(value);
            Long shopId = jsonObject.getLong("id");
            String name = jsonObject.getStr("name");
            String logoUrl = jsonObject.getStr("logoUrl");
            Integer type = jsonObject.getInt("type");
            Integer state = jsonObject.getInt("state");
            Integer environment = jsonObject.getInt("environment");
            Integer property = jsonObject.getInt("property");
            if (CommonConstant.State.STATE_START.equals(state)) {
                //现货价格和需要询价的商品只能在企业商城上架；积分兑换商品只能在积分商城上架；
                if (((PriceTypeEnum.Cash.getCode().equals(priceType) || PriceTypeEnum.Consult.getCode().equals(priceType) || PriceTypeEnum.Gift.getCode().equals(priceType)) && ShopTypeEnum.ENTERPRISE.getCode().equals(type)) || (PriceTypeEnum.Score.getCode().equals(priceType) && ShopTypeEnum.SCORE.getCode().equals(type))) {
                    CommodityShopResponse commodityShop = new CommodityShopResponse();
                    commodityShop.setShopId(shopId);
                    commodityShop.setEnvironment(environment);
                    commodityShop.setName(name);
                    commodityShop.setType(type);
                    commodityShop.setLogoUrl(logoUrl);
                    commodityShop.setIsSelfShop(isSelfShop);
                    commodityShop.setProperty(property);
                    //判断该商品是否已经上架
                    shopList.forEach(s -> {
                        if (s.getShopId() != null && shopId.equals(s.getShopId())) {
                            commodityShop.setIsCheck(true);
                        }
                    });
                    resultList.add(commodityShop);
                }
            }
        }
        return resultList;
    }

    /**
     * 获取上架商城(批量上架)--商品能力
     * @param simpleIdListRequest 商品id数组
     * @return
     */
    @RequestMapping(value = "/getShopBatch", method = RequestMethod.POST)
    public Wrapper<List<CommodityShopResponse>> getShopBatch(@RequestBody @Valid SimpleIdListRequest simpleIdListRequest) {
        List<Commodity> commodityList = commodityService.getCommodityByIdList(simpleIdListRequest.getIdList());
        List<Integer> priceTypeList = commodityList.stream().map(Commodity::getPriceType).distinct().collect(Collectors.toList());
        if(priceTypeList.size() == 1){
            Integer priceType = priceTypeList.get(0);
            Long memberId = commodityList.get(0).getMemberId();
            Long memberRoleId = commodityList.get(0).getMemberRoleId();
            //获取所有非自营商城
            List<CommodityShopResponse> commodityShopList = this.getCommodityShopList(priceType, Constants.REDIS_KEY_SHOP, false);
            //获取所有自营商城
            List<CommodityShopResponse> commoditySelfShopList = this.getCommodityShopList(priceType, Constants.REDIS_KEY_SELF_SHOP + "_" + memberId + "_" + memberRoleId, true);
            commodityShopList.addAll(commoditySelfShopList);
            return Wrapper.success(commodityShopList);
        }else{
            throw new BusinessException(ResponseCode.PRODUCT_PRICE_TYPE_EXCEPTION);
        }
    }

    /**
     * 获取商城(批量上架商品不需要勾中已上架的商城)
     */
    private List<CommodityShopResponse> getCommodityShopList(Integer priceType, String redisKey, Boolean isSelfShop){
        List<CommodityShopResponse> resultList = new ArrayList<>();
        //获取商品对应的上架商城
        Map<String, String> shopMap = redisStringUtils.hGetAll(redisKey, Constants.REDIS_PLATFORM_MANAGE_INDEX);
        //过滤企业商城和渠道商城，并且为启用的状态
        for (Map.Entry<String, String> entry : shopMap.entrySet()) {
            String value = entry.getValue();
            JSONObject jsonObject = JSONUtil.parseObj(value);
            Long shopId = jsonObject.getLong("id");
            String name = jsonObject.getStr("name");
            String logoUrl = jsonObject.getStr("logoUrl");
            Integer type = jsonObject.getInt("type");
            Integer state = jsonObject.getInt("state");
            Integer environment = jsonObject.getInt("environment");
            Integer property = jsonObject.getInt("property");
            if (CommonConstant.State.STATE_START.equals(state)) {
                //现货价格和需要询价的商品只能在企业商城上架；积分兑换商品只能在积分商城上架；
                if (((PriceTypeEnum.Cash.getCode().equals(priceType) || PriceTypeEnum.Consult.getCode().equals(priceType) || PriceTypeEnum.Gift.getCode().equals(priceType)) && ShopTypeEnum.ENTERPRISE.getCode().equals(type)) || (PriceTypeEnum.Score.getCode().equals(priceType) && ShopTypeEnum.SCORE.getCode().equals(type))) {
                    CommodityShopResponse commodityShop = new CommodityShopResponse();
                    commodityShop.setShopId(shopId);
                    commodityShop.setEnvironment(environment);
                    commodityShop.setName(name);
                    commodityShop.setType(type);
                    commodityShop.setLogoUrl(logoUrl);
                    commodityShop.setIsSelfShop(isSelfShop);
                    commodityShop.setProperty(property);
                    resultList.add(commodityShop);
                }
            }
        }
        return resultList;
    }

    /**
     * 上架商品--商品能力
     * @param commodityShopRequest
     * @return
     */
    @RequestMapping(value = "/publishCommodity", method = RequestMethod.POST)
    public Wrapper<String> publishCommodity(@RequestBody CommodityShopRequest commodityShopRequest) {
        return Wrapper.success(commodityService.publishCommodity(commodityShopRequest, true));
    }

    /**
     * 批量上架商品--商品能力
     * @param commodityShopBatchRequest
     * @return
     */
    @RequestMapping(value = "/publishCommodityBatch", method = RequestMethod.POST)
    public Wrapper<String> publishCommodityBatch(@RequestBody CommodityShopBatchRequest commodityShopBatchRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.publishCommodityBatch(sysUser, commodityShopBatchRequest, true));
    }

    /**
     * 下架商品--商品能力
     * @param commodityShopRequest
     * @return
     */
    @RequestMapping(value = "/offPublishCommodity", method = RequestMethod.POST)
    public Wrapper<String> offPublishCommodity(@RequestBody @Valid CommodityShopRequest commodityShopRequest) {
        return Wrapper.success(commodityService.publishCommodity(commodityShopRequest, false));
    }

    /**
     * 批量下架商品--商品能力
     * @param commodityShopBatchRequest
     * @return
     */
    @RequestMapping(value = "/offPublishCommodityBatch", method = RequestMethod.POST)
    public Wrapper<String> offPublishCommodityBatch(@RequestBody @Valid CommodityShopBatchRequest commodityShopBatchRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.publishCommodityBatch(sysUser, commodityShopBatchRequest, false));
    }

    /**
     * 查询规格商品属性--商品能力
     * @param unitPriceAndPicId 规格商品id
     * @return
     */
    @RequestMapping(value = "/getCommodityAttributeByUnitPriceAndPicId", method = RequestMethod.GET)
    public Wrapper<List<CommodityAttributeResponse>> getCommodityAttributeByUnitPriceAndPicId(@RequestParam("unitPriceAndPicId") Long unitPriceAndPicId) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        List<CommodityAttribute> commodityAttributeList = commodityService.getCommodityAttributeByUnitPriceAndPicId(sysUser, unitPriceAndPicId);
        List<CommodityAttributeResponse> resultList = this.modelMapper.map(commodityAttributeList, new TypeToken<List<CommodityAttributeResponse>>(){}.getType());
        return Wrapper.success(resultList);
    }

    /**
     * 查询商品列表(快捷修改商品单价)--商品能力
     * @param pageVO 分页实体
     * @param commodityQueryRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityDetailList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityGoodsResponse>> getCommodityDetailList(PageVO pageVO, CommodityQueryRequest commodityQueryRequest) {
        CommodityDetailRequest commodityDetailRequest = this.modelMapper.map(commodityQueryRequest, CommodityDetailRequest.class);
        UserLoginCacheDTO sysUser = this.getSysUser();
        commodityDetailRequest.setMemberId(sysUser.getMemberId());
        commodityDetailRequest.setMemberRoleId(sysUser.getMemberRoleId());
        List<Integer> priceTypeList = commodityQueryRequest.getPriceTypeList();
        if(priceTypeList == null || priceTypeList.isEmpty()){
            priceTypeList = new ArrayList<>();
            priceTypeList.add(PriceTypeEnum.Cash.getCode());
            priceTypeList.add(PriceTypeEnum.Score.getCode());
        }
        commodityDetailRequest.setPriceTypeList(priceTypeList);
        List<Integer> statusList = new ArrayList<>();
        statusList.add(CommodityStatusEnum.Pass.getCode());
        statusList.add(CommodityStatusEnum.On_Shelf.getCode());
        statusList.add(CommodityStatusEnum.Off_Shelf.getCode());
        commodityDetailRequest.setStatusList(statusList);
        return Wrapper.success(commodityService.getCommodityPriceList(pageVO, commodityDetailRequest));
    }

    /**
     * 获取商品价格/积分--商品能力
     * @param unitPriceAndPicId 关联表id
     * @return
     */
    @RequestMapping(value = "/getCommodityPrice", method = RequestMethod.GET)
    public Wrapper<Map<String, Double>> getCommodityPrice(@RequestParam("unitPriceAndPicId") Long unitPriceAndPicId) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.getCommodityPrice(sysUser, unitPriceAndPicId));
    }

    /**
     * 修改价格/积分--商品能力
     * @param unitPriceRequest
     * @return
     */
    @RequestMapping(value = "/updateCommodityPrice", method = RequestMethod.POST)
    public Wrapper<String> updateCommodityPrice(@RequestBody UnitPriceRequest unitPriceRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.updateCommodityPrice(sysUser, unitPriceRequest));
    }

    /**
     * 查询商品价格信息--商品能力
     * @param id 商品id
     * @return
     */
    @RequestMapping(value = "/getCommodityUnitPrice", method = RequestMethod.GET)
    public Wrapper<List<UnitPriceAndPicResponse>> getCommodityUnitPrice(@RequestParam Long id) {
        Commodity commodity = commodityService.getCommodity(id);
        if(commodity != null){
            List<CommodityUnitPriceAndPic> unitPriceAndPicList = commodity.getUnitPriceAndPicList();
            List<UnitPriceAndPicResponse> resultList = this.modelMapper.map(unitPriceAndPicList, new TypeToken<List<UnitPriceAndPicResponse>>(){}.getType());
            return Wrapper.success(resultList);
        }else{
            throw new BusinessException(ResponseCode.PRODUCT_COMMODITY_NOT_EXIST);
        }
    }

    /**
     * 查询价格策略列表--商品能力
     * @param pageVO 分页实体
     * @param unitPriceStrategyRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getUnitPriceStrategyList", method = RequestMethod.GET)
    public Wrapper<PageData<UnitPriceStrategyListResponse>> getUnitPriceStrategyList(PageVO pageVO, UnitPriceStrategyRequest unitPriceStrategyRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<CommodityUnitPriceStrategy> result = commodityService.getUnitPriceStrategyList(pageVO, sysUser, unitPriceStrategyRequest);
        List<UnitPriceStrategyListResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<UnitPriceStrategyListResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询商品列表(新增价格策略)--商品能力
     * @param pageVO 分页实体
     * @param unitPriceStrategyCommodity 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getStrategyCommodityList", method = RequestMethod.GET)
    public Wrapper<PageData<UnitPriceStrategyCommodityResponse>> getStrategyCommodityList(PageVO pageVO, UnitPriceStrategyCommodityRequest unitPriceStrategyCommodity) {
        CommodityQueryRequest commodityQueryRequest = new CommodityQueryRequest();
        commodityQueryRequest.setName(unitPriceStrategyCommodity.getName());
        commodityQueryRequest.setBrandId(unitPriceStrategyCommodity.getBrandId());
        commodityQueryRequest.setCustomerCategoryId(unitPriceStrategyCommodity.getCustomerCategoryId());
        commodityQueryRequest.setShopId(unitPriceStrategyCommodity.getShopId());
        commodityQueryRequest.setType(unitPriceStrategyCommodity.getType());
        commodityQueryRequest.setEnvironment(unitPriceStrategyCommodity.getEnvironment());
        //商品定价为现货价格或价格需要询价的商品
        List<Integer> priceTypeList = new ArrayList<>();
        priceTypeList.add(PriceTypeEnum.Cash.getCode());
        priceTypeList.add(PriceTypeEnum.Consult.getCode());
        commodityQueryRequest.setPriceTypeList(priceTypeList);
        //已上架的商品
        List<Integer> statusList = new ArrayList<>();
        statusList.add(CommodityStatusEnum.On_Shelf.getCode());
        commodityQueryRequest.setStatusList(statusList);

        UserLoginCacheDTO sysUser = this.getSysUser();
        commodityQueryRequest.setMemberId(sysUser.getMemberId());
        commodityQueryRequest.setMemberRoleId(sysUser.getMemberRoleId());
        Page<Commodity> result = commodityService.getCommodityList(pageVO, commodityQueryRequest);
        List<UnitPriceStrategyCommodityResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<UnitPriceStrategyCommodityResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 新增/修改价格策略--商品能力
     * @param unitPriceStrategyAddRequest 价格策略实体
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateUnitPriceStrategy", method = RequestMethod.POST)
    public Wrapper<Long> saveOrUpdateUnitPriceStrategy(@RequestBody @Valid UnitPriceStrategyAddRequest unitPriceStrategyAddRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.saveOrUpdateUnitPriceStrategy(sysUser, unitPriceStrategyAddRequest));
    }

    /**
     * 查询价格策略--商品能力
     * @param id 价格策略id
     * @return
     */
    @RequestMapping(value = "/getUnitPriceStrategy", method = RequestMethod.GET)
    public Wrapper<UnitPriceStrategyDetailResponse> getUnitPriceStrategy(@RequestParam("id") Long id){
        CommodityUnitPriceStrategy commodityUnitPriceStrategy = commodityService.getUnitPriceStrategy(id);
        return Wrapper.success(this.modelMapper.map(commodityUnitPriceStrategy, UnitPriceStrategyDetailResponse.class));
    }

    /**
     * 通过商品id获取价格策略的所有会员--商品能力
     * @param commodityId 商品id
     * @return
     */
    @RequestMapping(value = "/getStrategyMemberByCommodityId", method = RequestMethod.GET)
    public Wrapper<List<UnitPriceMemberResponse>> getStrategyMemberByCommodityId(@RequestParam("commodityId") Long commodityId){
        List<CommodityMember> result = commodityService.getStrategyMemberByCommodityId(commodityId);
        return Wrapper.success(this.modelMapper.map(result, new TypeToken<List<UnitPriceMemberResponse>>(){}.getType()));
    }

    /**
     * 启用/停用价格策略--商品能力
     * @param simpleStatusRequest
     * @return
     */
    @RequestMapping(value = "/updateUnitPriceStrategyStatus", method = RequestMethod.POST)
    public Wrapper<String> updateUnitPriceStrategyStatus(@RequestBody SimpleStatusRequest simpleStatusRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.updateUnitPriceStrategyStatus(sysUser, simpleStatusRequest.getId(), simpleStatusRequest.getIsEnable()));
    }

    /**
     * 删除价格策略--商品能力
     * @param simpleIdRequest
     * @return
     */
    @RequestMapping(value = "/deleteUnitPriceStrategy", method = RequestMethod.POST)
    public Wrapper<String> deleteUnitPriceStrategy(@RequestBody SimpleIdRequest simpleIdRequest){
        return Wrapper.success(commodityService.deleteUnitPriceStrategy(simpleIdRequest.getId()));
    }

    /**
     * 通过商城id查询商品sku列表
     * @param couponCommodityRequest 实体
     * @return
     */
    @RequestMapping(value = "/getCommoditySkuListByShopId", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySkuResponse>> getCommoditySkuListByShopId(@RequestBody CouponCommodityRequest couponCommodityRequest) {
        List<Long> shopIdList = couponCommodityRequest.getShopIdList();
        String commodityName = couponCommodityRequest.getCommodityName();
        Long brandId = couponCommodityRequest.getBrandId();
        Long customerCategoryId = couponCommodityRequest.getCustomerCategoryId();
        Boolean isMorePrice = couponCommodityRequest.getIsMorePrice();
        List<Long> idNotInList = couponCommodityRequest.getIdNotInList();

        UserLoginCacheDTO sysUser = this.getSysUser();
        Integer memberType = sysUser.getMemberType();

        CommodityDetailRequest commodityDetailRequest = new CommodityDetailRequest();
        commodityDetailRequest.setMemberId(sysUser.getMemberId());
        commodityDetailRequest.setMemberRoleId(sysUser.getMemberRoleId());
        //商城
        commodityDetailRequest.setShopIdList(shopIdList);

        //商品类型
        List<Integer> priceTypeList = new ArrayList<>();
        Boolean isGift = couponCommodityRequest.getIsGift();
        if(isGift != null && isGift){
            //赠品
            priceTypeList.add(PriceTypeEnum.Gift.getCode());
        }else{
            //现货商品
            priceTypeList.add(PriceTypeEnum.Cash.getCode());
        }
        commodityDetailRequest.setPriceTypeList(priceTypeList);

        //已上架
        List<Integer> statusList = new ArrayList<>();
        statusList.add(CommodityStatusEnum.On_Shelf.getCode());
        commodityDetailRequest.setStatusList(statusList);

        //查询条件
        commodityDetailRequest.setCustomerCategoryId(customerCategoryId);
        commodityDetailRequest.setBrandId(brandId);
        commodityDetailRequest.setName(commodityName);
        commodityDetailRequest.setIsMorePrice(isMorePrice);
        commodityDetailRequest.setIdNotInList(idNotInList);

        PageVO pageVO = new PageVO();
        pageVO.setCurrent(couponCommodityRequest.getCurrent());
        pageVO.setPageSize(couponCommodityRequest.getPageSize());
        if(MemberTypeEnum.MERCHANT.getCode().equals(memberType) || MemberTypeEnum.MERCHANT_PERSONAL.getCode().equals(memberType)){
            return Wrapper.success(commodityService.getCommoditySkuListByShopId(pageVO, commodityDetailRequest));
        }else{
            return Wrapper.success(channelCommodityService.getChannelCommoditySkuListByShopId(pageVO, commodityDetailRequest));
        }
    }

    /**
     * 查询商品历史价格
     * @param commoditySkuId  商品skuId
     * @param days         最近多少天
     * @return 商品历史价格
     */
    @RequestMapping(value = "/getPriceRecord", method = RequestMethod.GET)
    public Wrapper<CommodityPriceRecordResponse> getPriceRecord(@RequestParam("commoditySkuId") Long commoditySkuId, @RequestParam("days") Integer days) {
        return Wrapper.success(commodityService.getPriceRecord(commoditySkuId, days));
    }

    /**
     * 查询常购清单列表
     * @param pageVO 分页实体
     * @param name 商品名称
     * @return 常购清单列表
     */
    @RequestMapping(value = "/getOftenBuyCommodityList", method = RequestMethod.GET)
    public Wrapper<PageData<OftenBuyCommodityResponse>> getOftenBuyCommodityList(PageVO pageVO, @RequestParam(value = "name", required = false) String name) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.getOftenBuyCommodityList(pageVO, shopId, name, sysUser));
    }

    /**
     * 导出商品二维码
     * @param simpleIdListRequest 商品id数组
     * @return 压缩包
     */
    @RequestMapping(value = "/exportCommodityQRCode", method = RequestMethod.POST)
    public void exportCommodityQRCode(@RequestBody @Valid SimpleIdListRequest simpleIdListRequest, HttpServletRequest request, HttpServletResponse response) {
        //一次性只能导出1000个商品二维码
        List<Long> commodityIdList = simpleIdListRequest.getIdList();
        int size = commodityIdList.size();
        if(size > 1000){
            throw new BusinessException(ResponseCode.PRODUCT_COMMODITY_EXPORT_QRCODE_SIZE);
        }
        //查询商品信息并按商城id分组
        Map<Long, List<Commodity>> commodityMap = commodityService.getCommodityListGroupByShopId(commodityIdList);
        //查询redis中的商城信息
        List<Long> shopIdList = new ArrayList<>(commodityMap.keySet());
        List<String> stringList = shopIdList.stream().map(String::valueOf).collect(Collectors.toList());
        List<String> redisList = redisStringUtils.hMGet(Constants.REDIS_KEY_SHOP, ArrayUtil.toArray(stringList, String.class), Constants.REDIS_PLATFORM_MANAGE_INDEX);
        Map<Long, String> shopMap = new HashMap<>();
        if(redisList != null && redisList.size() > 0){
            redisList.forEach(s -> {
                JSONObject jsonObject = JSONUtil.parseObj(s);
                Long shopId = jsonObject.getLong("id");
                String shopName = jsonObject.getStr("name");
                shopMap.put(shopId, shopName);
            });
        }
        try{
            //生成临时根目录
            String fileName = "commodityQRCode";
            String realPath = request.getSession().getServletContext().getRealPath("/");
            String path = realPath + fileName;
            File file = new File(path);
            if (!file.exists()) {
                boolean flag = file.mkdirs();
                if(!flag){
                    throw new BusinessException(ResponseCode.SERVICE_ERROR);
                }
            }
            //遍历商品信息
            for (Map.Entry<Long, List<Commodity>> entry : commodityMap.entrySet()) {
                Long shopId = entry.getKey();
                List<Commodity> shopIdCommodityList = entry.getValue();
                String shopName = shopMap.get(shopId);
                //生成对应的临时目录
                File shopFile = new File(path + File.separator + shopName + "_" + DateUtil.format(new Date(), "YYYYMMdd"));
                if (!shopFile.exists()) {
                    boolean flag = shopFile.mkdirs();
                    if(!flag){
                        throw new BusinessException(ResponseCode.SERVICE_ERROR);
                    }
                }
                for (Commodity commodity : shopIdCommodityList) {
                    Long commodityId = commodity.getId();
                    String commodityName = commodity.getName();
                    Integer priceType = commodity.getPriceType();
                    //生成二维码
                    QRCodeUtil.createQRCodeImage("shopId=" + shopId + "&commodityId=" + commodityId + "&priceType=" + priceType, 400, 400, shopFile + File.separator + commodityId + "_" + commodityName + ".jpg", "jpg");
                }
            }
            //生成zip文件
            String zipFileName = path + ".zip";
            ZipUtil.zipCompress(path, zipFileName);
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".zip", "utf-8"));
            InputStream in = new FileInputStream(zipFileName);
            OutputStream out = response.getOutputStream();
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            out.flush();
            //删除多余文件
            FileUtil.deleteDir(new File(path));
            in.close();
            FileUtil.deleteDir(new File(zipFileName));
            out.close();
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(ResponseCode.SERVICE_ERROR);
        }
    }

    /**
     * 查询商品草稿列表
     * @param pageVO 分页实体
     */
    @RequestMapping(value = "/getCommodityDraftList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityDraftResponse>> getCommodityDraftList(PageVO pageVO) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        PageData<CommodityDraftResponse> commodityDraftList = commodityService.getCommodityDraftList(sysUser, pageVO);
        return Wrapper.success(commodityDraftList);
    }

    /**
     * 查询商品草稿
     * @param commodityDraftId 商品草稿id
     */
    @RequestMapping(value = "/getCommodityDraftById", method = RequestMethod.GET)
    public Wrapper<CommodityDraftResponse> getCommodityDraftById(@RequestParam("commodityDraftId") Long commodityDraftId) {
        return Wrapper.success(commodityService.getCommodityDraftById(commodityDraftId));
    }

    /**
     * 保存商品草稿
     */
    @RequestMapping(value = "/saveOrUpdateCommodityDraft", method = RequestMethod.POST)
    public Wrapper<Long> saveOrUpdateCommodityDraft(@RequestBody @Valid CommodityDraftRequest commodityDraftRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.saveOrUpdateCommodityDraft(sysUser, commodityDraftRequest));
    }

    /**
     * 删除商品草稿
     */
    @RequestMapping(value = "/deleteCommodityDraftBatch", method = RequestMethod.POST)
    public Wrapper<Boolean> deleteCommodityDraftBatch(@RequestBody @Valid SimpleIdListRequest simpleIdListRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.deleteCommodityDraft(sysUser, simpleIdListRequest.getIdList()));
    }

    /**
     * 导出商品导入模板
     * @return 导入模板
     */
    @RequestMapping(value = "/exportCommodityTemplate", method = RequestMethod.GET)
    public void exportCommodityTemplate(HttpServletRequest request, HttpServletResponse response) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        commodityService.exportCommodityTemplate(sysUser, request, response);
    }

    /**
     * 导入商品
     * @param file  商品文件
     * @return 导入情况说明
     */
    @RequestMapping(value = "/importCommodity" , method = RequestMethod.POST)
    public Wrapper<Boolean> importCommodity(MultipartFile file){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.importCommodity(sysUser, file));
    }

    /**
     * 查询商品导入批次
     * @param name 商品导入批次
     */
    @RequestMapping(value = "/getCommodityImportBath", method = RequestMethod.GET)
    public Wrapper<List<String>> getCommodityImportBath(@RequestParam(value = "name", required = false) String name) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.getCommodityImportBath(sysUser, name));
    }

    /**
     * 通过商品导入批次删除商品
     * @param importBath 商品导入批次
     */
    @RequestMapping(value = "/deleteCommodityByImportBath", method = RequestMethod.POST)
    public Wrapper<Boolean> deleteCommodityByImportBath(@RequestBody @Valid CommodityImportBathRequest importBath) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.deleteCommodityByImportBath(sysUser, importBath.getCommodityImportBath()));
    }

    /**
     * 导出商品sku纬度数据
     */
    @RequestMapping(value = "/exportCommoditySkuByCommodityIdList", method = RequestMethod.POST)
    public void exportCommoditySkuByCommodityIdList(@RequestBody @Valid SimpleIdListRequest simpleIdListRequest, HttpServletResponse response) {
        List<Long> commodityIdList = simpleIdListRequest.getIdList();
        commodityService.exportCommoditySkuByCommodityIdList(response, commodityIdList);
    }

    /**
     * 通过商品id数组查询对应的商品skuId数组
     * @param simpleIdListRequest 商品id数组
     */
    @RequestMapping(value = "/getCommoditySkuIdList", method = RequestMethod.POST)
    public Wrapper<List<CommodityIdAndSkuIdListResponse>> getCommoditySkuIdList(@RequestBody @Valid SimpleIdListRequest simpleIdListRequest) {
        return Wrapper.success(commodityService.getCommoditySkuIdList(simpleIdListRequest.getIdList()));
    }

    /**
     * 查询商品是否配置了库存
     * @param simpleIdListRequest 商品id数组
     * @return 返回没有配置库存的商品id数组
     */
    @RequestMapping(value = "/getCommodityIsExistStock", method = RequestMethod.POST)
    public Wrapper<List<Long>> getCommodityIsExistStock(@RequestBody @Valid SimpleIdListRequest simpleIdListRequest) {
        return Wrapper.success(commodityService.getCommodityIsExistStock(simpleIdListRequest.getIdList()));
    }

    /**
     * 查询商品列表--通过会员商品sku集合
     * @param simpleIdListRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityByCommoditySkuIdList", method = RequestMethod.POST)
    public Wrapper<List<CommodityGoodsResponse>> getCommodityByCommoditySkuIdList(@RequestBody @Valid SimpleIdListRequest simpleIdListRequest) {
        List<Long> commoditySkuIdList = simpleIdListRequest.getIdList();
        return Wrapper.success(commodityService.getCommodityByCommoditySkuIdList(commoditySkuIdList));
    }
    /**
     * 判断货品是否与商品关联
     *
     * @param request 请求参数
     * @return Boolean
     */
    @RequestMapping(value = "/goodsUsing", method = RequestMethod.GET)
    public Wrapper<Boolean> goodsUsing(SimpleIdListRequest request) {
        return Wrapper.success(commodityService.goodsUsing(request));
    }

    /************************************************  平台后台  ********************************************************/

    /**
     * 查询商品列表--平台后台
     * @param pageVO 分页实体
     * @param commodityPlatformRequest
     * @return
     */
    @RequestMapping(value = "/getPlatformCommodityList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityPlatformResponse>> getPlatformCommodityList(PageVO pageVO, CommodityPlatformRequest commodityPlatformRequest) {
        Integer status = commodityPlatformRequest.getStatus();
        Integer priceType = commodityPlatformRequest.getPriceType();
        CommodityQueryRequest commodityQueryRequest = this.modelMapper.map(commodityPlatformRequest, CommodityQueryRequest.class);
        //状态
        List<Integer> statusList = new ArrayList<>();
        if(status != null && status > 0){
            statusList.add(status);
        }else{
            statusList.add(CommodityStatusEnum.Not_Check.getCode());
            statusList.add(CommodityStatusEnum.Not_Pass.getCode());
            statusList.add(CommodityStatusEnum.Pass.getCode());
            statusList.add(CommodityStatusEnum.On_Shelf.getCode());
            statusList.add(CommodityStatusEnum.Off_Shelf.getCode());
        }
        commodityQueryRequest.setStatusList(statusList);
        //产品类型
        List<Integer> priceTypeList = new ArrayList<>();
        if(priceType != null && priceType > 0){
            priceTypeList.add(priceType);
        }
        commodityQueryRequest.setPriceTypeList(priceTypeList);

        Page<Commodity> result = commodityService.getPlatformCommodityList(pageVO, commodityQueryRequest);
        List<CommodityPlatformResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<CommodityPlatformResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }
    /**
     * 查询待审核商品列表--平台后台
     * @param pageVO 分页实体
     * @param commodityPlatformRequest 请求参数
     * @return 操作结果
     */
    @RequestMapping(value = "/getPlatformUnCheckCommodityList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityPlatformResponse>> getPlatformUnCheckCommodityList(PageVO pageVO, CommodityPlatformRequest commodityPlatformRequest) {
        Integer status = commodityPlatformRequest.getStatus();
        Integer priceType = commodityPlatformRequest.getPriceType();
        CommodityQueryRequest commodityQueryRequest = this.modelMapper.map(commodityPlatformRequest, CommodityQueryRequest.class);
        //状态
        List<Integer> statusList = new ArrayList<>();
        if(status != null && status > 0){
            statusList.add(status);
        }else{
            statusList.add(CommodityStatusEnum.Not_Check.getCode());
            statusList.add(CommodityStatusEnum.Not_Pass.getCode());
            statusList.add(CommodityStatusEnum.Pass.getCode());
            statusList.add(CommodityStatusEnum.On_Shelf.getCode());
            statusList.add(CommodityStatusEnum.Off_Shelf.getCode());
        }
        commodityQueryRequest.setStatusList(statusList);
        //产品类型
        List<Integer> priceTypeList = new ArrayList<>();
        if(priceType != null && priceType > 0){
            priceTypeList.add(priceType);
        }
        commodityQueryRequest.setPriceTypeList(priceTypeList);
        commodityQueryRequest.setStatusList(Collections.singletonList(2));
        commodityQueryRequest.setCheckType(CheckTypeEnum.PLATFORM_CHECK.getCode());
        Page<Commodity> result = commodityService.getPlatformCommodityList(pageVO, commodityQueryRequest);
        List<CommodityPlatformResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<CommodityPlatformResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 审核商品--平台后台
     * @param commodityCheckRequest
     * @return
     */
    @RequestMapping(value = "/checkCommodity", method = RequestMethod.POST)
    public Wrapper<String> checkCommodity(@RequestBody CommodityCheckRequest commodityCheckRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityService.checkCommodity(sysUser, commodityCheckRequest.getId(), commodityCheckRequest.getStatus(), commodityCheckRequest.getCheckRemark()));
    }

    /**
     * 查询商品审核记录--平台后台
     * @param commodityId 商品id
     */
    @RequestMapping(value = "/getCommodityCheckRecord", method = RequestMethod.GET)
    public Wrapper<List<CommodityCheckRecord>> getCommodityCheckRecord(@RequestParam("commodityId") Long commodityId) {
        return Wrapper.success(commodityService.getCommodityCheckRecord(commodityId));
    }

    /**
     * 查询商品审核记录--操作日志
     * @param commodityId 商品id
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    @RequestMapping(value = "/getCommodityCheckRecordList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityCheckRecord>> getCommodityCheckRecordList(PageVO pageVO, @RequestParam(value = "commodityId",required = false) Long commodityId, @RequestParam(value = "startTime", required = false) Long startTime, @RequestParam(value = "endTime",required = false) Long endTime) {
        Page<CommodityCheckRecord> result = commodityService.getCommodityCheckRecordList(pageVO, commodityId, startTime, endTime);
        return Wrapper.success(new PageData<>(result.getTotalElements(), result.getContent()));
    }
}
